package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import dao.AdminDao;
import dao.ItemDao;
import domain.Item;
import domain.User;

public class ItemServiceImpl implements ItemService {
	/*
	 * 
	 * ����Item��ݣ�����date��id��ʱû��
	 */
	private ItemDao itemDao;
	private AdminDao adminDao;
	
	
	public AdminDao getAdminDao() {
		return adminDao;
	}


	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}


	public ItemDao getItemDao() {
		return itemDao;
	}


	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}
	
	@Override
	public void insertItem(Item Item,String acc,String path){
		User user = (User)adminDao.getUserByAccount(acc).get(0);
		user.getSellItem().add(Item);
		Item.setOwner(user);
		Item.setCreateDate(new Date());
		itemDao.insertSellItem(Item,path);
		
	}
	
	@Override
	public List getAllItem(String myS,String acc,String sellOrBuy) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		if(myS.equals("fan")){
			list = itemDao.getFansItem(acc,sellOrBuy);
		}
		else if(myS.equals("follow")){
			list = itemDao.getFanedItem(acc,sellOrBuy);
		}
		else{
		list = itemDao.getAllItem(sellOrBuy);
		}
		//HibernateSessionFactory.closeSession();
		return list;
	}
	
	@Override
	public List getPersonAllItem(String myS,int userId,String sellOrBuy) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		if(myS.equals("fan")){
			list = itemDao.getPersonFansItem(userId,sellOrBuy);
		}
		else if(myS.equals("follow")){
			list = itemDao.getPersonFanedItem(userId,sellOrBuy);
		}
		else{
		list = itemDao.getPersonItem(sellOrBuy,userId);
		}
		//HibernateSessionFactory.closeSession();
		return list;
	}
	
	@Override
	public Item insertItemLookUser(String acc,String item_id){
		Item item = itemDao.insertItemLookUser(acc, item_id);
//		System.out.println(item);
		return item;
	}


	@Override
	public List<Item> queryItem(String q,String sellOrBuy) {
		// TODO Auto-generated method stub
		return itemDao.queryItem(q,sellOrBuy);
	}


	@Override
	public List showOwnerItem(String acc,String sellOrBuy) {
		// TODO Auto-generated method stub
		List list = itemDao.showOwnerItem(acc,sellOrBuy);
		return list;
	}


	@Override
	public Item updateItem(String item_id, String ownSellOrBuy) {
		// TODO Auto-generated method stub
		return itemDao.updateItem(item_id,ownSellOrBuy);
	}


	@Override
	public int updateItemOperate(Item item) {
		// TODO Auto-generated method stub
		return itemDao.updateItemOperate(item);
	}


	@Override
	public void deleteItem(String item_id) {
		// TODO Auto-generated method stub
		itemDao.deleteItem(item_id);
	}


	@Override
	public List showKeyWord() {
		// TODO Auto-generated method stub
		return itemDao.showKeyWord();
	}


	@Override
	public List getPersonItem(int userId, String sellOrBuy) {
		// TODO Auto-generated method stub
		return itemDao.getPersonItem(sellOrBuy, userId);
	}
}
